﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Threading;

namespace WebeTerm.Common
{
    public class WebIMClientAsyncResult : IAsyncResult
    {

        bool m_IsCompleted = false;
        private HttpContext m_Context;
        private AsyncCallback m_Callback;
        private object m_ExtraData;
        private string m_Content;
        private string m_LoginID = string.Empty;


        public WebIMClientAsyncResult(HttpContext p_Context, AsyncCallback p_Callback, object p_ExtraData)
        {
            this.m_Context = p_Context;
            this.m_Callback = p_Callback;
            this.m_ExtraData = p_ExtraData;
        }
        /// <summary>
        /// 用户编号
        /// </summary>
        public string LoginID
        {
            get
            {
                return m_LoginID;
            }
            set
            {
                m_LoginID = value;
            }
        }
        /// <summary>
        /// 发送消息的内容，暂时未使用到
        /// </summary>
        public string Content
        {
            get
            {
                return m_Content;
            }
            set
            {
                m_Content = value;
            }
        }
        #region IAsyncResult 成员

        public object AsyncState
        {
            get { return null; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { return null; }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return m_IsCompleted; }
        }

        #endregion

        /// <summary>
        /// 向客户端响应消息
        /// </summary>
        /// <param name="data"></param>
        public void Send(object data)
        {
            try
            {
                m_Context.Response.Write(this.Content);
                if (m_Callback != null)
                {
                    m_Callback(this);
                }

            }
            catch { }
            finally
            {
                m_IsCompleted = true;
            }
        }
    }
}
